package com.jxsd.edu.cn.struts2.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.jxsd.edu.cn.struts2.models.LabelView;
import com.jxsd.edu.cn.struts2.serviceImpl.LabelServiceImpl;
import com.jxsd.edu.cn.struts2.services.LabelService;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.opensymphony.xwork2.ActionContext;


/**
 * 标签Action
 * */
public class LabelAction 
{
	/**
	 * 一条标签
	 * */
	private   LabelView  label;
	/**
	 * 一组兴趣标签
	 * */
	private   Page<LabelView>  page;
	/**
	 * 分页请求器
	 * */
	private  PageBean<LabelView>  pagebean;
	//labelService
	 private   LabelService  labelService=new LabelServiceImpl();
	
	

	/**
	 * LabelService
	 * */
	private   LabelService  service=new  LabelServiceImpl();
	
   /**
    * 获取一组Label标签
    * */
	public  void  queryLabels()
	{
		pagebean.setPageSize(15);
		page=service.getLabels(pagebean);
		 HttpServletResponse response=ServletActionContext.getResponse();
 		response.setContentType("text/plain");
 		response.setCharacterEncoding("UTF-8");
 		
 		StringBuffer  buffer=new   StringBuffer();
 		for(LabelView  label:page.getList())
 		{
 			buffer.append(label.getSmallName());
 			buffer.append("/");
 		}
 		String  s=buffer.toString();
 		s=s.substring(0, s.lastIndexOf("/"));
 		
 		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.write(s);
	 		writer.flush();
	 		writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除一条用户的兴趣标签
	 * */
	public  void  deleteLabel()
	{
		service.deleteLabel(Integer.toString(label.getLabelId()));	
	}
	
	/**
	 * 添加一组兴趣标签
	 * */
	public   String  addUserLabels()
	{
		int userId=(Integer)ActionContext.getContext().getSession().get("userId");
		
		HttpServletRequest request=ServletActionContext.getRequest();
		String lables=request.getParameter("labels");
		if(null==lables||lables.length()<1)
		{
			return "success";
		}
		
		lables=lables.replaceAll("x","").trim();
		
		//去除用户当前所填兴趣标签和数据库中兴趣标签相重复的部分
		List<LabelView>  labs=queryPersonLabels();
		for(LabelView  l:labs)
		{
			lables=lables.replaceAll(l.getSmallName(), "");
		}
		
		System.out.println(lables);
		//获取用户填写的兴趣标签
		String[]  strs=lables.split("/");
		
		List<LabelView>  list=new  ArrayList<LabelView>();
		for(String s:strs)
		{
			LabelView  lab=new  LabelView();
			lab.setSmallName(s);
			lab.setUserId(userId);
			list.add(lab);
		}
		service.addLabels(list);
		
		return  "success";
	}
	
	/**
	    * 获取用户的兴趣标签
	    * */
	   private  List<LabelView>  queryPersonLabels()
	   {
		   List<LabelView>  list=null;
		   int userId=(Integer)ActionContext.getContext().getSession().get("userId");
		   list=labelService.getLabelViews(Integer.toString(userId));
		   return  list;
	   }
	
	/**
	 * 添加一条用户的兴趣标签
	 * */
	public  void  addLabel()
	{
		service.addLabel(label);
	}

	public LabelView getLabel() {
		return label;
	}

	public void setLabel(LabelView label) {
		this.label = label;
	}

	public Page<LabelView> getPage() {
		return page;
	}

	public void setPage(Page<LabelView> page) {
		this.page = page;
	}
	
	public PageBean<LabelView> getPagebean() {
		return pagebean;
	}

	public void setPagebean(PageBean<LabelView> pagebean) {
		this.pagebean = pagebean;
	}
	
}
